package com.anhry.www.safety.manage.system.struts.action;

import java.util.Date;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.enterprise.bean.TbCorpPerson;
import com.anhry.www.safety.manage.system.struts.base.SystemBaseAction;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.Util;
import com.anhry.www.safety.util.UtilMethod;


/**
 * @date:2012-5-15
 * @remark:
 * @author:dongbin
 */
@Controller
@Scope("prototype")
public class LoginAction extends SystemBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String corpName;
	private String password;
	private String rand;
	
	public String execute(){
		TbCorpInfo corp = null;
		try {	
			
			String url = this.contextPvd.getRequestURL();//访问网址
			String param = this.contextPvd.getRequest().getQueryString();//参数
			if(UtilMethod.urlFilter(url) || UtilMethod.urlFilter(param)){
				return ERROR;
			}
			if(null==this.corpName || "".equals(this.corpName)){
				this.setTip("用户名输入错误，系统无此用户名！");
				return INPUT;
			}
			//验证码
			if(this.contextPvd.getSessionAttr("rand") != null && !this.getRand().equalsIgnoreCase(this.contextPvd.getSessionAttr("rand").toString())){
				this.setTip("验证码错误，请重新输入！");
				return INPUT;
			}	
			this.contextPvd.getSession().removeAttribute("rand");
			corp = this.systemService.getCorpByCorpName(this.corpName.trim());
			if(corp == null){
				this.setTip("企业名称输入错误，系统无此企业！");
				return INPUT;
			}
			corp = this.systemService.corpLogin(corp, this.getPassword());
			if(corp != null){
				/**
				 * 将登录企业放到session中
				 */
				TbCorpPerson person =this.enterpriseService.getCorpSafetyHead(corp.getId(),"1");
				if(person!=null){
					corp.setSafetyHead(person.getPerName());
				}
				TbCorpPerson fr =this.enterpriseService.getCorpSafetyHead(corp.getId(),"1");
				if(fr!=null){
					corp.setCorpFr(fr.getPerName());
					corp.setCorpFrTel(fr.getPerPhone());
					corp.setCorpFrMobile(fr.getPerMobile());
				}
				
				contextPvd.setSessionAttr(Util.SESSION_CORPUSER,corp);
				/**
				 * 记录用户登录信息
				 */
				corp.setCorpLoginTime(new Date());
				corp.setCorpLoginCount(corp.getCorpLoginCount()+1);
				this.utilService.update(corp);
				return SUCCESS;
			}else{
				this.setTip("密码输入错误，请重新输入！");
				return INPUT;			
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("manage:LoginAction企业登录错误"+e.getMessage());
		}
		return INPUT;
	}


	public String getCorpName() {
		return corpName;
	}


	public void setCorpName(String corpName) {
		this.corpName = corpName;
	}


	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}

}
